//
//  SSAddressListController.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/3/9.
//

import UIKit

import SwiftyJSON

public typealias AddressListClosure = (_ finish:SSAddress)->()


class SSAddressListController: UIViewController {
    @IBOutlet weak var addAddressBtn: UIButton!
    
    @IBOutlet weak var tableView: SSBaseTableView!
    var addressListClosure: AddressListClosure?     // 闭包
    var userOrderController: SSBaseUserOrderController?     // 
//    var user = SSUser()
    

    var items: Array<SSAddress> = []

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUIs()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        getListNetwork()

    }

    func setupUIs() {
        title = "我的地址"
        addAddressBtn.layer.cornerRadius = 8
        addAddressBtn.layer.masksToBounds = true
        
        tableView.register(UINib(nibName: "SSAddressCell", bundle: Bundle.init(for: SSAddressCell.self)), forCellReuseIdentifier: "SSAddressCell")
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.nullView.textLab.text = "暂无可用的服务地址"
    }
    
    func closure(_ finished: @escaping AddressListClosure) {
        addressListClosure = finished
    }
    
    @IBAction func addAddress(_ sender: Any) {
        let sb = UIStoryboard(name: "SSAddressViewController", bundle: nil)
        let vc = sb.instantiateInitialViewController() as! SSAddressViewController
        vc.addressListController = self
        navigationController?.pushViewController(vc, animated: true)
    }
    
}

extension SSAddressListController {
    
    func getListNetwork() {
        let param = [
                    "phoneNo": kUserPhone
        ] as [String:AnyObject]
        SSProgressHUD.share.show()
        HttpConnectTools.share.request(urlString: APIUrl.share.getUserAddress_URL, parameters: param) { (result, error) in
            SSProgressHUD.share.dismiss()

            if let _ = result{
                let json = JSON(parseJSON: result!)
                let data = json["data"]["listAddress"].arrayValue
                self.items.removeAll()
                for obj in data {
                    let address = SSAddress()
                    address.id = obj["id"].stringValue
                    address.sex = obj["sex"].stringValue
                    address.address = obj["address"].stringValue
                    address.houseNo = obj["houseNo"].stringValue
                    address.zip = obj["zip"].stringValue
                    address.contact = obj["contact"].stringValue
                    address.contactPhone = obj["contactPhone"].stringValue
                    self.items.append(address)
                }
                self.tableView.isHiddenNullView(isHiden: !(self.items.count == 0))
                self.tableView.reloadData()

            }else{
//                if let message = error?.userInfo["errorinfo"]{
//                    self.view.makeToast(message as? String)
//                }
            }
        }
    }
    
}

extension SSAddressListController: UITableViewDelegate, UITableViewDataSource
{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        if (self.addressListClosure != nil) {
            self.addressListClosure!(self.items[indexPath.row])
            UIViewController.current().navigationController?.popViewController(animated: true)
        }else{
            let sb = UIStoryboard(name: "SSAddressViewController", bundle: nil)
            let vc = sb.instantiateInitialViewController() as! SSAddressViewController
            vc.addressListController = self
            vc.address = self.items[indexPath.row]
            navigationController?.pushViewController(vc, animated: true)
        }
    }

    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SSAddressCell") as! SSAddressCell
        cell.setData(address: self.items[indexPath.row])
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 75
    }
    
    
}
